#include "TempSensor1021.h"
#include "ChineseEnglishMapping.h"
#include <QDebug>
#include <QVBoxLayout>
#include <QSplitter>

TempSensor1021::TempSensor1021()
{

}

void TempSensor1021::InitUI(const QString &fileName, QList<QPair<QWidget *, QString> > &widgets, QMap<int, QList<TableWidgetForm *> > &m_pTableWidgetFormMap)
{
    QString filePath = QString("%1/config/%2/%3")
            .arg(COMMONS->XmlPath)
            .arg(COMMONS->MachType)
            .arg(fileName);
    TempIOConnectStr _TempIOConnectStr;
    QList<QList<TempSensor>> TempSensorList;
    Analysis(filePath, _TempIOConnectStr, TempSensorList);

    //添加第一个表
    QWidget* widget = new QWidget;
    QVBoxLayout *layout = new QVBoxLayout(widget);
    QSplitter *splitter = new QSplitter(Qt::Vertical, widget);
    splitter->setChildrenCollapsible(false);
    layout->addWidget(splitter);
    widgets.append(qMakePair(widget,QStringLiteral("tab0")));

    TableWidgetForm* _TableWidgetForm = new TableWidgetForm(widget);
    splitter->addWidget(_TableWidgetForm);
    _TableWidgetForm->setGroupBoxTitle(QString("%1_0").arg(fileName.left(fileName.size()-4)));

    //获取表头
    _TableWidgetForm->setTableWidgetHeaderLabels(COMMONS->getTableWidgetHeadLabels(fileName,_TempIOConnectStr.toFieldNames().split(',')));
    _TableWidgetForm->setTableWidgetHeaderType(_TempIOConnectStr.toFieldTypes());

    _TableWidgetForm->getTableWidget()->setRowCount(1);
    _TableWidgetForm->getTableWidget()->setItem(0, 0, new QTableWidgetItem(_TempIOConnectStr.name));
    _TableWidgetForm->getTableWidget()->setItem(0, 1, new QTableWidgetItem(_TempIOConnectStr.type));

    m_pTableWidgetFormMap.insert(0,  QList<TableWidgetForm*>() << _TableWidgetForm);

    for (int i = 0;i<TempSensorList.size();i++)
    {
        TableWidgetForm* _TableWidgetForm = new TableWidgetForm(widget);
        splitter->addWidget(_TableWidgetForm);
        _TableWidgetForm->setGroupBoxTitle(QString("%1_%2").arg(fileName.left(fileName.size()-4)).arg(i+1));

        //获取表头
        TempSensor _TempSensortemp;
        _TableWidgetForm->setTableWidgetHeaderLabels(COMMONS->getTableWidgetHeadLabels(fileName, _TempSensortemp.toFieldNames().split(',')));
        _TableWidgetForm->setTableWidgetHeaderType(_TempSensortemp.toFieldTypes());

        for (auto item : TempSensorList.at(i))
        {
            int rowCount = _TableWidgetForm->getTableWidget()->rowCount();
            _TableWidgetForm->getTableWidget()->setRowCount(rowCount+1);
            _TableWidgetForm->getTableWidget()->setItem(rowCount, 0, new QTableWidgetItem(item.name));
            _TableWidgetForm->getTableWidget()->item(rowCount,0)->setData(Qt::UserRole,item.id);
            _TableWidgetForm->getTableWidget()->setItem(rowCount, 1, new QTableWidgetItem(QString("%1").arg(item.alarm)));
            _TableWidgetForm->getTableWidget()->setItem(rowCount, 2, new QTableWidgetItem(QString("%1").arg(item.limit)));
            _TableWidgetForm->getTableWidget()->setItem(rowCount, 3, new QTableWidgetItem(QString("%1").arg(item.board_id)));
            _TableWidgetForm->getTableWidget()->setItem(rowCount, 4, new QTableWidgetItem(QString("%1").arg(item.phyid)));
            _TableWidgetForm->getTableWidget()->setItem(rowCount, 5, new QTableWidgetItem(QString("%1").arg(item.port)));
        }
        QList<TableWidgetForm*> item = m_pTableWidgetFormMap.value(0);
        m_pTableWidgetFormMap.insert(0, item << _TableWidgetForm);
    }
}

bool TempSensor1021::SaveUI(const QString &fileName, const QMap<int, QList<TableWidgetForm *> > &m_pTableWidgetFormMap)
{
    QString filePath = QString("%1/config/%2/%3")
            .arg(COMMONS->XmlPath)
            .arg(COMMONS->MachType)
            .arg(fileName);
    if(m_pTableWidgetFormMap.count() > 0)
    {
        TempIOConnectStr _TempIOConnectStr;
        QList<QList<TempSensor>> TempSensorList;

        TableWidgetForm* _TableWidgetForm = m_pTableWidgetFormMap.value(0).at(0);
        if(_TableWidgetForm)
        {
            _TempIOConnectStr.name = _TableWidgetForm->getTableWidget()->item(0, 0)->text();
            _TempIOConnectStr.type = _TableWidgetForm->getTableWidget()->item(0, 1)->text();
        }
        for (int i = 1;i<m_pTableWidgetFormMap.value(0).size();i++)
        {
            QList<TempSensor> _TempSensors;
            TableWidgetForm* _TableWidgetForm = m_pTableWidgetFormMap.value(0).at(i);
            if(_TableWidgetForm)
            {
                for(int k = 0;k<_TableWidgetForm->getTableWidget()->rowCount();k++)
                {
                    TempSensor _TempSensor;
                    _TempSensor.id = _TableWidgetForm->getTableWidget()->item(k,0)->data(Qt::UserRole).toInt();
                    _TempSensor.name = _TableWidgetForm->getTableWidget()->item(k,0)->text();
                    _TempSensor.alarm = _TableWidgetForm->getTableWidget()->item(k,1)->text().toInt();
                    _TempSensor.limit = _TableWidgetForm->getTableWidget()->item(k,2)->text().toInt();
                    _TempSensor.board_id = _TableWidgetForm->getTableWidget()->item(k,3)->text().toInt();
                    _TempSensor.phyid = _TableWidgetForm->getTableWidget()->item(k,4)->text().toInt();
                    _TempSensor.port = _TableWidgetForm->getTableWidget()->item(k,5)->text().toInt();
                    _TempSensors.append(_TempSensor);
                }
            }
            TempSensorList.append(_TempSensors);
        }

        SaveXml(filePath,_TempIOConnectStr,TempSensorList);
        return true;
    }
    return false;
}
